<template>
  <a-modal
    :title="title"
    :width="460"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="取消"
    okText="保存"
    wrapClassName="ant-modal-cust-warp">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="返修部位">
          <a-input placeholder="请输入要返修的部位" v-model="huchi_name"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="排列序号"
          hasFeedback >
          <a-input placeholder="请输入排列序号" v-model="huchi_numbers"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="单位"
          hasFeedback >
          <a-input placeholder="请输入返修单位" v-model="huchi_cunit"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="返修范围"
          hasFeedback >
          <a-input placeholder="返修最小值" v-model="huchi_min" style="width: 120px;float: left"/>
          <hr style="float: left;width: 20px;margin-top: 15px;background-color: #f2f2f2;margin-left: 5px">
          <a-input placeholder="返修最大值" v-model="huchi_max" style="width: 120px;float: left;margin-left: 5px"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="备注"
          hasFeedback >
          <a-textarea placeholder="请输入备注信息" v-model="remark" rows="4"/>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction, postAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"

  export default {
    name: "JeecgDemoModal",
    components: {
    },
    data () {
      return {
        huchi_name:'',
        huchi_numbers:'',
        huchi_cunit:'',
        huchi_min:'',
        huchi_max:'',
        remark:'',
        dataDetail:[],//数据对象
        title:"操作",
        visible: false,
        model: {},
        validatorRules:{
          roleName:{
            rules: [
              { required: true, message: '请输入角色名称!' },
              { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' }
            ]},
          roleCode:{
            rules: [
              { required: true, message: '请输入角色名称!'},
              { min: 0, max: 64, message: '长度不超过 64 个字符', trigger: 'blur' },
              { validator: this.validateRoleCode}
            ]},
          description:{
            rules: [
              { min: 0, max: 126, message: '长度不超过 126 个字符', trigger: 'blur' }
            ]}
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        statuslist: [
          {
            id: '1',
            name: '允许'
          },
          {
            id: '2',
            name: '禁止'
          }
        ],
        confirmLoading: false,
        form: this.$form.createForm(this),
        url: {
          query_detail:"/admin/parameter/test_fanxiuDetail",
          add: "/admin/parameter/add_fanxiubodysize",
          edit: "/admin/parameter/update_fanxiu"
        },
      }
    },
    computed: {

    },
    created () {

    },
    methods: {
      edit (record) {
        this.huchi_name='',
          this.huchi_numbers='',
          this.huchi_cunit='',
          this.huchi_min='',
          this.huchi_max='',
          this.remark='',
          this.model = Object.assign({}, record);
        this.visible = true;//开启弹框
        if(record=="0"){
          this.title="新增返修"
        }else {
          this.title="编辑返修"
          this.show_datainfodetail(this.model.id);
        }
      },
      show_datainfodetail(id) {//查询详情
        let params = {id:id};
        getAction(this.url.query_detail,params).then((res)=>{
          if(res.success){
            this.huchi_name=res.result.bodySizename;
            this.huchi_numbers=res.result.numbers;
            this.huchi_cunit=res.result.unit;
            this.huchi_min=res.result.min;
            this.huchi_max=res.result.max;
            this.remark=res.result.remark;
          }else {
            this.$message.error("查询失败2");
          }
        });
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
          if(this.huchi_name==null || this.huchi_name==''){
            this.$message.warning("请输入返修部位名称");
            return;
          }else if(this.huchi_numbers==='' || this.huchi_numbers===null){
            this.$message.warning("请输入排列序号");
            return;
          }else if(!reg.test(this.huchi_numbers)){
            this.$message.warning("请输入正确格式的排列序号");
            return;
          }
          else if(this.huchi_cunit=='' || this.huchi_cunit==null){
            this.$message.warning("请输入返修单位");
            return;
          }else if(this.huchi_min=='' || this.huchi_min==null){
            this.$message.warning("请输入返修最小值");
            return;
          }else if(!reg.test(this.huchi_min)){
            this.$message.warning("请输入正确格式的返修最小值")
            return;
          }
          else if(this.huchi_max=='' || this.huchi_max==null){
            this.$message.warning("请输入返修最大值");
            return;
          }else if(!reg.test(this.huchi_max)){
            this.$message.warning("请输入正确格式的返修最大值")
            return;
          }
          var minnum = this.huchi_min*1;
          var maxnum = this.huchi_max*1;
          if(minnum>maxnum){
            this.$message.warning("返修最大值不能小于最小值")
            return;
          }
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            let params = '';
            if(this.model.id!=null && this.model.id!=''){
              httpurl+=this.url.edit;
              method = 'put';
              params = {bodySizename:this.huchi_name,unit:this.huchi_cunit,numbers:this.huchi_numbers,
                max:this.huchi_max,min:this.huchi_min,remark:this.remark,id:this.model.id}
            }else{
              httpurl+=this.url.add;
              method = 'post';
              params = {bodySizename:this.huchi_name,unit:this.huchi_cunit,numbers:this.huchi_numbers,
                max:this.huchi_max,min:this.huchi_min,remark:this.remark,
                categoryId:this.$route.query.category_id}
            }
            httpAction(httpurl,params,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.$emit('ok');
              that.close();
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },
    }
  }
</script>

<style scoped>

</style>